package org.jzs.mybaseapp.section.home.costomview;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;
import org.jzs.mybaseapp.R;
import org.jzs.mybaseapp.common.utils.AppLog;
import org.jzs.mybaseapp.common.widget.expandtab.ExpandTabView;
import org.jzs.mybaseapp.section.home.entity.ExpandTabEntity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class ExpandTabActivity extends Activity {

    private ExpandTabView expandTabView;
    private ExpandTabEntity entity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expand_tab);
        expandTabView = (ExpandTabView) findViewById(R.id.expandtab_view);
        readFromAssets();
        initExpandList();

    }

    public void initExpandList() {
        expandTabView.removeAllViews();
        ArrayList<String> mTextArray = new ArrayList<String>();
        ArrayList<View> mViewArray = new ArrayList<View>();

        QyView mQyView = new QyView(this, entity.data.area);
        LxView mLxView = new LxView(this, entity.data.cat, "60");
        PxView mPxView = new PxView(this, entity.data.sort);

        mViewArray.add(mQyView);
        mTextArray.add("区域");

        mViewArray.add(mLxView);
        mTextArray.add("类型");

        mViewArray.add(mPxView);
        mTextArray.add("排序");

        expandTabView.setValue(mTextArray, mViewArray);

        expandTabView.setTitle("区域", 0);
        expandTabView.setTitle("类型", 1);
        expandTabView.setTitle("智能排序", 2);

    }


    //读取assets中的文件
    private void readFromAssets() {
        try {
            InputStream is = getAssets().open("city.txt");//此处为要加载的json文件名称
            String text = readTextFromSDcard(is);
            AppLog.e(text);

            Gson gson = new Gson();
            try {
                entity = gson.fromJson(text, ExpandTabEntity.class);
            } catch (Exception e) {

            }

//            handleCitiesResponse(text);
        } catch (Exception e) {
            Log.d("readFromAssets", e.toString());
        }
    }

    //将传入的is一行一行解析读取出来出来
    private String readTextFromSDcard(InputStream is) throws Exception {
        InputStreamReader reader = new InputStreamReader(is, "GB2312");
        BufferedReader bufferedReader = new BufferedReader(reader);
        StringBuffer buffer = new StringBuffer("");
        String str;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
            buffer.append("\n");
        }
        return buffer.toString();//把读取的数据返回
    }

}
